package com.rk.school.controller;

import com.rk.school.dao.StudentInfoMapper;
import com.rk.school.form.UploadFileForm;
import com.rk.school.model.BaseResult;
import com.rk.school.model.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static com.rk.school.constants.Constants.UPLOADED_FOLDER;

/**
 * 作者：请叫我百米冲刺 on 2019/12/16 11:54 AM
 * 邮箱：mail@hezhilin.cc
 */
@RestController
@RequestMapping("api")
public class NetController {

    @Autowired
    public StudentInfoMapper mapper;

    @RequestMapping(path = "/")
    String home() {
        return "Hello World!";
    }

    // 新增一条数据
    @RequestMapping(path = "/insert", method = {RequestMethod.POST})
    public BaseResult insert(String name, Integer age, Integer sex) {
        StudentInfo info = new StudentInfo();
        info.setName(name);
        info.setAge(age);
        info.setSex(sex);
        int number = mapper.insert(info);
        BaseResult baseResult = new BaseResult();
        if (number == 0) {
            baseResult.setCode(10001);
            baseResult.setMessage("数据更新失败");
        }
        return baseResult;
    }

    // 删除一条数据
    @RequestMapping(path = "/deleteById", method = {RequestMethod.GET})
    public BaseResult deleteById(Integer id) {
        int number = mapper.deleteByPrimaryKey(id);
        BaseResult baseResult = new BaseResult();
        if (number == 0) {
            baseResult.setCode(10002);
            baseResult.setMessage("数据删除失败");
        }
        return baseResult;
    }

    // 修改一条数据
    @RequestMapping(path = "/updateById", method = {RequestMethod.POST})
    public BaseResult updateById(StudentInfo newInfo) {
        int number = mapper.updateByPrimaryKeySelective(newInfo);
        BaseResult baseResult = new BaseResult();
        if (number == 0) {
            baseResult.setCode(10003);
            baseResult.setMessage("数据更新失败");
        }
        return baseResult;
    }

    // 根据id查询用户
    @RequestMapping(path = "/selectById", method = {RequestMethod.GET})
    public BaseResult selectById(Integer id) {
        StudentInfo info = mapper.selectByPrimaryKey(id);
        BaseResult baseResult = new BaseResult();
        if (info == null) {
            baseResult.setCode(10004);
            baseResult.setMessage("数据不存在");
        }
        baseResult.setData(info);
        return baseResult;
    }

    // 查询所有的
    @RequestMapping(path = "/selectAll", method = {RequestMethod.GET})
    public BaseResult selectAll() {
        return new BaseResult(mapper.selectAll());
    }

    // 上传文件相关
    @RequestMapping(value = "/upload")
    public BaseResult uploadImage(UploadFileForm uploadFileForm) {
        MultipartFile file = uploadFileForm.getFile();
        BaseResult baseResult = new BaseResult();
        if (file == null || file.isEmpty()) { //文件为空
            baseResult.setCode(10005);
            baseResult.setMessage("文件不能为空");
        }else{
            try {
                //进行上传的操作
                byte[] bytes = file.getBytes();
                Path path = Paths.get(UPLOADED_FOLDER + File.separator + file.getOriginalFilename());
                Files.write(path, bytes);
            } catch (IOException e) {
                e.printStackTrace();
                baseResult.setCode(10006);
                baseResult.setMessage("文件上传失败");
                baseResult.setData(null);
            }
        }
        return baseResult;
    }

    // 下载文件相关
    @RequestMapping(path = "/{fileName}")
    public void download(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        String fullPath = UPLOADED_FOLDER + File.separator + fileName;
        File downloadFile = new File(fullPath);
        try (InputStream is = new FileInputStream(downloadFile);
             OutputStream os = response.getOutputStream()) {
            byte[] buffer = new byte[1024]; // 文件缓存池
            while (is.read(buffer) != -1) {
                os.write(buffer);
            }
            os.flush();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}
